﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace SlamCms.Common.Internal
{
	internal class PostScriptImageExtracter
	{   
		[DllImport("gsdll32.dll", EntryPoint = "gsapi_new_instance")]
		private static extern int CreateAPIInstance(out IntPtr pinstance, IntPtr caller_handle);

		[DllImport("gsdll32.dll", EntryPoint = "gsapi_init_with_args")]
		private static extern int InitAPI(IntPtr instance, int argc, string[] argv);

		[DllImport("gsdll32.dll", EntryPoint = "gsapi_exit")]
		private static extern int ExitAPI(IntPtr instance);

		[DllImport("gsdll32.dll", EntryPoint = "gsapi_delete_instance")]
		private static extern void DeleteAPIInstance(IntPtr instance);

		public static void SaveImage(string documentPath, int page, string imagePath)
		{
			SaveImages(documentPath, page, page, imagePath);
		}

		public static void SaveImages(string documentPath, int firstPage, int lastPage, string imagePath)
		{
			string[] arguments;
			if (documentPath.Contains(".eps"))
			{
				arguments = new[]
				{   
					"-q",						//Prevent GhostScript from writing to standard output
					"-dQUIET",
				   
					"-dPARANOIDSAFER",			//Run in safe mode
					"-dBATCH",					//Prevent GhostScript from going into interactive mode
					"-dNOPAUSE",				//Do not pause for each page
					"-dNOPROMPT",				//Disable prompts for user interaction           
					"-dMaxBitmap=500000000",	//Set high for better performance
					
					String.Format("-dFirstPage={0}", firstPage),
					String.Format("-dLastPage={0}", lastPage),   
					
					"-dAlignToPixels=0",
					"-dGridFitTT=0",
					"-sDEVICE=jpeg",
					"-dEPSCrop",				//Crop EPS
					"-dTextAlphaBits=4",
					"-dGraphicsAlphaBits=4",
					
					String.Format("-sOutputFile={0}", imagePath),
					documentPath
				};
			}
			else
			{
				arguments = new[]
				{
					 "-q",						//Prevent GhostScript from writing to standard output
					"-dQUIET",
				   
					"-dPARANOIDSAFER",			//Run in safe mode
					"-dBATCH",					//Prevent GhostScript from going into interactive mode
					"-dNOPAUSE",				//Do not pause for each page
					"-dNOPROMPT",				//Disable prompts for user interaction           
					"-dMaxBitmap=500000000",	//Set high for better performance
					
					String.Format("-dFirstPage={0}", firstPage),
					String.Format("-dLastPage={0}", lastPage),   
					
					"-dAlignToPixels=0",
					"-dGridFitTT=0",
					"-sDEVICE=jpeg",
					"-dTextAlphaBits=4",
					"-dGraphicsAlphaBits=4",
					
					String.Format("-sOutputFile={0}", imagePath),
					documentPath
				};
			}

			IntPtr ghostScriptPointer;
			CreateAPIInstance(out ghostScriptPointer, IntPtr.Zero);
			InitAPI(ghostScriptPointer, arguments.Length, arguments);

			Cleanup(ghostScriptPointer);
		}
		
		private static void Cleanup(IntPtr ghostScriptPointer)
		{
			ExitAPI(ghostScriptPointer);
			DeleteAPIInstance(ghostScriptPointer);
		}
	}
}
